<?php

/*
 * This file is part of the FOSUserBundle package.
 *
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace IMT\AccountBundle\Form;

use IMT\AccountBundle\Entity\Account;

use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\EntityManager;

class AccountFormHandler
{
    protected $form;
    protected $request;
    protected $em;
    
    public function __construct(Form $form, Request $request, EntityManager $em)
    {
    	$this->form    = $form;
    	$this->request = $request;
    	$this->em      = $em;
    }

    public function process($submit = true)
    {
        if ('POST' == $this->request->getMethod()) {
            $this->form->bindRequest($this->request);

            if ($this->form->isValid()) {
            	if($submit)
                	$this->onSuccess($this->form->getData());

                return true;
            }
        }

        return false;
    }

    public function onSuccess(Account $account)
    {
        $this->em->persist($account);
        $this->em->flush();
    }
}
